package com.piesat.quake.common.mqtt.decoder.common;

import com.alibaba.druid.sql.visitor.functions.Char;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * 解码器
 *
 * @param <T>
 */
public abstract class CommonDecoder<T> {

    private byte[] bytes;
    private ByteBuf byteBuf;

    public CommonDecoder(byte[] bytes) {
        this.bytes = bytes;
        this.byteBuf = Unpooled.copiedBuffer(bytes);
    }

    public abstract T decoder();

    protected String readString(int length) {
        byte[] tmpBytes = new byte[length];
        this.byteBuf.readBytes(tmpBytes);
        return new String(tmpBytes);
    }

    protected Float readFloat() {
        return this.byteBuf.readFloat();
    }


    protected Float readFloatLE() {
        return this.byteBuf.readFloatLE();
    }

    protected Integer readInteger() {
        return this.byteBuf.readInt();
    }

    protected Integer readIntegerLE() {
        return this.byteBuf.readIntLE();
    }

    protected Byte readByte() {
        return this.byteBuf.readByte();
    }

    protected Double readDouble() {
        return this.byteBuf.readDouble();
    }

    protected Double readDoubleLE() {
        return this.byteBuf.readDoubleLE();
    }

    protected Long readLong() {
        return this.byteBuf.readLong();
    }

    protected Long readLongLE() {
        return this.byteBuf.readLongLE();
    }

    protected Short readShort() {
        return this.byteBuf.readShort();
    }

    protected Short readShortLE() {
        return this.byteBuf.readShortLE();
    }

    protected char readChar() {
        return this.byteBuf.readChar();
    }

}
